package howl.Servlets;

import howl.model.Handler;
import howl.model.Model;
import howl.types.HowlUser;
import howl.types.query.QueryHelper;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import compiler.HowlCompiler;
import compiler.HowlToken;
import compiler.exceptions.HowlModelException;
import compiler.exceptions.HowlParserException;
import compiler.exceptions.HowlUnauthorisedAction;
import compiler.nonterminals.AlarmStmt;
import compiler.nonterminals.CreateStmt;
import compiler.nonterminals.DeleteStmt;
import compiler.nonterminals.InsertStmt;
import compiler.nonterminals.ReportStmt;

@SuppressWarnings("serial")
public class HowlApiServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		System.out.println(req);
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String com = req.getParameter("command");

	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		System.out.println(req);
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		HowlUser hu = Model.getHowlUserfromUser(user);
		String com = req.getParameter("command");
		HowlCompiler compiler = new HowlCompiler();
		try {
			List<HowlToken> ht = compiler.parse(com);
			Handler h = new Handler(hu);
			for (HowlToken t : ht) {
				h.Handle(t);
				if(t instanceof ReportStmt) {
					ReportStmt r = (ReportStmt)t;
					if(r.getTop() != null && r.getFreq() == null) {
						resp.setContentType("text/plain");
						resp.getWriter().println(QueryHelper.queryByEntity(r.getFeedname().getFeedname(), r.getTop().intValue()).toXML());	
					}
				}
			}
			
			if(ht.size() >1) {
				resp.sendError(HttpServletResponse.SC_OK);
			} else {
				HowlToken t = ht.get(0);
				if(t instanceof CreateStmt) {
					resp.sendError(HttpServletResponse.SC_CREATED);
				} else if(t instanceof DeleteStmt) {
					resp.sendError(HttpServletResponse.SC_OK);
				} else if(t instanceof InsertStmt) {
					resp.sendError(HttpServletResponse.SC_OK);
				} else if(t instanceof ReportStmt) {
	
				}else if(t instanceof AlarmStmt) {
						resp.sendError(HttpServletResponse.SC_ACCEPTED);
					}
			}

		} catch (HowlParserException e) {
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
		} catch (HowlModelException e1) {
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		} catch(HowlUnauthorisedAction e2) {
			resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The current google user account is not authorized to complete this action");
		}
	}
}
